package com.jingan.nursinghome.regulatory.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import cn.android.support.v7.lib.sin.crown.utils.AssetsUtils;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;

/**
 * Created by 彭治铭 on 2017/9/29.
 */

public class TabView extends View {
    public TabView(Context context) {
        super(context);
        init();
    }

    public TabView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    int width = ProportionUtils.getInstance().adapterInt(680);
    int height = ProportionUtils.getInstance().adapterInt(265);

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(width, height);
    }

    Paint paint;

    Bitmap bitmap, more;
    String infos;//"千分表", "日常表", "专项表"
    int textSize = height / 5;

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setTextSize(textSize);
        paint.setColor(Color.parseColor("#333333"));
        setLayerType(View.LAYER_TYPE_HARDWARE, paint);
        more = AssetsUtils.getInstance().getBitmapFromAssets( "按钮/more.png", 0, true);
        more = ProportionUtils.getInstance().adapterBitmap(more);
    }

    int category = 0;//0千分表，1日常表，2专项表

    public void setCategory(int category) {
        this.category = category;
        switch (category) {
            case 0:
                bitmap = AssetsUtils.getInstance().getBitmapFromAssets( "图标/千分表.png", 0, true);
                bitmap = ProportionUtils.getInstance().adapterBitmap(bitmap);
                infos = "千分表";
                break;
            case 1:
                bitmap = AssetsUtils.getInstance().getBitmapFromAssets( "图标/日常表.png", 0, true);
                bitmap = ProportionUtils.getInstance().adapterBitmap(bitmap);
                infos = "日常表";
                break;
            case 2:
                bitmap = AssetsUtils.getInstance().getBitmapFromAssets( "图标/专项表.png", 0, true);
                bitmap = ProportionUtils.getInstance().adapterBitmap(bitmap);
                infos = "专项表 ";
                break;
        }
        invalidate();
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        if (bitmap == null || infos == null) {
            return;
        }
        canvas.drawColor(Color.parseColor("#FFFFFF"));
        int left = (int) (width / 3.6);
        int right=width - left;
        int textWidth=(int)paint.measureText(infos,0,infos.length());
        canvas.drawBitmap(bitmap, left, (height - bitmap.getHeight()) / 2, paint);
        canvas.drawText(infos, width/2+textSize / 2, height / 2 + textSize / 3, paint);
        canvas.drawBitmap(more, right, (height - more.getHeight()) / 2, paint);
    }
}
